package com.sdr.hkcamera;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;

import com.hikvision.vmsnetsdk.CameraInfo;
import com.hikvision.vmsnetsdk.ControlUnitInfo;
import com.hikvision.vmsnetsdk.RegionInfo;
import com.hikvision.vmsnetsdk.ServInfo;
import com.hikvision.vmsnetsdk.VMSNetSDK;
import com.sdr.hkcamera.data.Node;
import com.sdr.hkcamera.data.TempData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by homelajiang on 2016/9/26 0026.
 */

public class HKPresenter implements HKContract.Presenter {


    private HKContract.View view;
    private Handler handler = new Handler();
    List<Node> datas;


    @Override
    public void attach(Context context, HKContract.View view) {
        this.view = view;
    }

    @Override
    public void login(String serverIP, String username, String password, String macAddr) {

        new AsyncTask<String, Void, Boolean>() {

            @Override
            protected Boolean doInBackground(String... params) {
                ServInfo servInfo = new ServInfo();
                boolean ret = VMSNetSDK.getInstance().login(params[0], params[1], params[2], params[3], servInfo);
                if (ret) {
                    TempData.getInstance().setLoginData(servInfo);
                    TempData.getInstance().setServerAddr(params[0]);
                }
                return ret;
            }

            @Override
            protected void onPostExecute(Boolean aBoolean) {
                if (aBoolean) {
                    view.loginSuccess();
                } else {
                    view.loginFail();
                }
            }
        }.execute(serverIP, username, password, macAddr);
    }

    @Override
    public void loadNodeList() {
        datas = new ArrayList<Node>();

        final ServInfo loginData = TempData.getIns().getLoginData();
        final String sessionID = loginData.getSessionID();
        final int numPerPage = 10000;
        final int curPage = 1;
        final String serverAddr = TempData.getInstance().getServerAddr();


        new AsyncTask<Void, Void, Boolean>() {

            @Override
            protected Boolean doInBackground(Void... params) {


                Node node = new Node();
                node.setType(Node.NODE_UNKNOW);
                boolean res = requestSubRes(node);
                TempData.getInstance().setNodeList(datas);
                return res;
            }

            boolean requestSubRes(Node node) {
                if (node.getType() == Node.NODE_UNKNOW) {

                    List<ControlUnitInfo> ctrlUnitList = new ArrayList<ControlUnitInfo>();
                    boolean ret = VMSNetSDK.getInstance().getControlUnitList(serverAddr
                            , sessionID, String.valueOf(0), numPerPage,
                            curPage, ctrlUnitList);
                    if (!ret) {
                        return ret;
                    }
                    for (ControlUnitInfo info : ctrlUnitList) {
                        Node temp = new Node(info, 0, 0);
                        datas.add(temp);
                        requestSubRes(temp);
                    }

                } else if (node.getType() == Node.NODE_UNIT) {

                    List<ControlUnitInfo> ctrlUnitList = new ArrayList<ControlUnitInfo>();
                    boolean ret;
                    if (node.getLevel() < 4) {//控制中心结构最多5层
                        // 1.从控制中心获取控制中心
                        ret = VMSNetSDK.getInstance().getControlUnitList(serverAddr, sessionID,
                                String.valueOf(node.getId()), numPerPage,
                                curPage, ctrlUnitList);
                        if (!ret)
                            return false;

                        for (ControlUnitInfo info : ctrlUnitList) {
                            Node nodeUnit = new Node(info, node.getLevel() + 1, node.getId());
                            datas.add(nodeUnit);
                            requestSubRes(nodeUnit);
                        }

                        List<RegionInfo> regionList = new ArrayList<RegionInfo>();
                        // 2.从控制中心获取区域列表
                        ret = VMSNetSDK.getInstance().getRegionListFromCtrlUnit(serverAddr, sessionID,
                                String.valueOf(node.getId()), numPerPage,
                                curPage, regionList);
                        if (!ret)
                            return false;

                        for (RegionInfo info : regionList) {
                            Node nodeRegion = new Node(info, node.getLevel() + 1, node.getId());
                            datas.add(nodeRegion);
                            requestSubRes(nodeRegion);
                        }

                    }

                    List<CameraInfo> cameraList = new ArrayList<CameraInfo>();
                    // 3.从控制中心获取摄像头列表
                    ret = VMSNetSDK.getInstance().getCameraListFromCtrlUnit(serverAddr, sessionID,
                            String.valueOf(node.getId()), numPerPage,
                            curPage, cameraList);
                    if (!ret)
                        return false;
                    for (CameraInfo info : cameraList) {
                        Node nodeCamera = new Node(info, node.getLevel() + 1, node.getId());
                        datas.add(nodeCamera);
                    }

                } else if (node.getType() == Node.NODE_REGION) {
                    boolean ret;
                    if (node.getLevel() < 4) {//控制中心结构最多5层
                        List<RegionInfo> regionList = new ArrayList<RegionInfo>();
                        // 1.从区域获取区域列表
                        ret = VMSNetSDK.getInstance().getRegionListFromRegion(serverAddr, sessionID
                                , String.valueOf(node.getId()), numPerPage, curPage, regionList);

                        if (!ret)
                            return false;

                        for (RegionInfo info : regionList) {
                            Node nodeRegion = new Node(info, node.getLevel() + 1, node.getId());
                            datas.add(nodeRegion);
                            requestSubRes(nodeRegion);
                        }
                    }


                    List<CameraInfo> cameraList = new ArrayList<CameraInfo>();
                    // 2.从区域获取监控点（摄像头）列表
                    ret = VMSNetSDK.getInstance().getCameraListFromRegion(serverAddr, sessionID,
                            String.valueOf(node.getId()), numPerPage, curPage,
                            cameraList);

                    if (!ret)
                        return false;

                    for (CameraInfo info : cameraList) {
                        Node cameraNode = new Node(info, node.getLevel() + 1, node.getId());
                        datas.add(cameraNode);
                    }
                } else if (node.getType() == Node.NODE_CAMERA) {

                } else {
                    throw new IllegalArgumentException("节点类型未定义");
                }
                return true;
            }

            @Override
            protected void onPostExecute(Boolean aBoolean) {
                view.loadNodeResult(aBoolean);
            }
        }.execute();
    }
}
